package com.mkono.infra.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mkono.infra.BuildConfig;
import com.mkono.infra.R;
import com.mkono.infra.untils.UserUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by huang on 2019/12/8.
 */

@ContentView(R.layout.activity_welcome)
public class Welcome extends BaseActivity {

    PopupWindow systemPolicy;
//    @ViewInject(R.id.welcome)
//    ImageView welcome;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        if(Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            //让应用主题内容占用系统状态栏的空间,注意:下面两个参数必须一起使用 stable 牢固的
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //设置状态栏颜色为透明
            getWindow().setStatusBarColor(getResources().getColor(R.color.colorAccent));
        }
        x.view().inject(this);
        init();
    }

    @Override
    public void initView() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                if(!UserUtil.getUserAgree(getContext())){
                    Looper.prepare();
                    View welcome = LayoutInflater.from(getContext()).inflate(R.layout.activity_welcome, null);
                    View systemInfo = getLayoutInflater().inflate(R.layout.pop_systempolicy, null);
                    systemPolicy = new PopupWindow(systemInfo, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                    TextView policytext = systemInfo.findViewById(R.id.policytext);
                    policytext.setMovementMethod(ScrollingMovementMethod.getInstance());
                    //取消按钮触发事件
                    TextView policy_cancl = systemInfo.findViewById(R.id.policy_cancl);
                    policy_cancl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Welcome.this.finish();
                        }
                    });
                    //统一按钮触发事件
                    TextView policy_agree = systemInfo.findViewById(R.id.policy_agree);
                    policy_agree.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            UserUtil.setUserAgree(getContext(),true);
                            Intent tabMain = new Intent();
                            tabMain.setClass(Welcome.this,MainActivity.class);
                            //charge.putExtra("state","charge");
                            startActivity(tabMain);
                            Welcome.this.finish();
                        }
                    });
                    //查看用户协议
                    TextView user_policy = systemInfo.findViewById(R.id.user_policy);
                    user_policy.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent policy = new Intent();
                            policy.setClass(Welcome.this,UserAgreeAvtivity.class);
                            startActivity(policy);
                        }
                    });
                    //隐私政策
                    TextView privacy_policy = systemInfo.findViewById(R.id.privacy_policy);
                    privacy_policy.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent policy = new Intent();
                            policy.setClass(Welcome.this,UserPolicyAvtivity.class);
                            startActivity(policy);
                        }
                    });
                    systemPolicy.setFocusable(false);
                    systemPolicy.showAtLocation(welcome, Gravity.BOTTOM, 0, 0);
                    Looper.loop();
                } else {
                    Intent tabMain = new Intent();
                    tabMain.setClass(Welcome.this,MainActivity.class);
                    startActivity(tabMain);
                    Welcome.this.finish();
                }

            }
        }, 3000);

    }
}
